package com.mk.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil
{
	public static final SimpleDateFormat ym = new SimpleDateFormat("yyyy-MM");
	public static final SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat ymd_hms = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	/**
	 * 月份-季度
	 */
	public static final Map<Integer,Integer> month_quarter= new HashMap<>(12);
	static {
		month_quarter.put(1, 1);
		month_quarter.put(2, 1);
		month_quarter.put(3, 1);

		month_quarter.put(4, 2);
		month_quarter.put(5, 2);
		month_quarter.put(6, 2);

		month_quarter.put(7, 3);
		month_quarter.put(8, 3);
		month_quarter.put(9, 3);

		month_quarter.put(10, 4);
		month_quarter.put(11, 4);
		month_quarter.put(12, 4);
		
	}
	
	/**
	 * 季度-月
	 */
	public static final Map<Integer, Integer[]> quarter_months = new HashMap<>(4);
	static {
		quarter_months.put(1, new Integer[]{1, 2, 3});
		quarter_months.put(2, new Integer[]{4, 5, 6});
		quarter_months.put(3, new Integer[]{7, 8, 9});
		quarter_months.put(4, new Integer[]{10, 11, 12});
	}
	
	
	/**
	 * 获取指定日期前几个月
	 * lg: (2019-10-23,3) res: {2019-07, 2019-08, 2019-09}
	 * @param tarDate
	 * @param num
	 * @return
	 */
	public static String[] getDatesBefore(Date tarDate, int num) {
		if(tarDate==null || num<0)
			return null;
		
		String[] result = new String[num];  //结果
		
		Calendar calendar =  Calendar.getInstance();
		calendar.setTime(tarDate);
		int year = calendar.get(Calendar.YEAR); //年
		int month = calendar.get(Calendar.MONTH);//月
		for(int i=1; i<=num; i++) {
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month-i);
			
			Date tempDate = calendar.getTime();
			result[num-i] = ym.format(tempDate);
		}
		
		return result;
	}
	
	/**
	 * 获取指定日期的 下个季度第一个月
	 * @param tarDate
	 * @return yyyy-MM
	 */
	public static String getFirstMonthOfNextQuarter(Date tarDate) {
		//获取月份
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(tarDate);
		int month = calendar.get(Calendar.MONTH); //当前月份
		calendar.set(Calendar.MONTH, month+3);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		
		//下个季度
		int year_ = calendar.get(Calendar.YEAR); //年份
		int month_= calendar.get(Calendar.MONTH)+1; //月份
		int quarter = month_quarter.get(month_); //季度
		Integer[] months = quarter_months.get(quarter);
		
		
		return year_+"-"+StringUtils.getStandLengthString(months[0]+"", 2);
	}
	
	/**
	 * 获取上个月
	 * @param today
	 * @return
	 */
	public static String getLastMonth(Date today) {
		//获取实例
		Calendar calendar = Calendar.getInstance();
		//设置时间
		calendar.setTime(today);
		//设置月、日
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		
		//获取日期
		Date lastDate = calendar.getTime();
		return DateUtil.ym.format(lastDate);
	}
	
	
}
